<?php
/**
 * shopify平台销售
 * User: bgl
 * Date: 2022/9/7
 * Time: 18:50
 */

namespace App\Admin\Controllers\Products;


use App\Admin\Actions\Products\ShopifySkuCCS\ImportShopifySkuCCS;
use App\Exporter\Products\ShopifySkuCCSExporter;
use App\Models\Basics\SysCountry;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductShopifySkuCCS;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class ShopifySkuCCSController extends AdminController{

    protected $title = 'Shopify销售管理';

    protected function grid()
    {
        $grid = new Grid(new ProductShopifySkuCCS());
        $grid->column('country.country', '国家');
        $grid->column('classify.season', '季节性')->using(ProductClassify::$_season)->style('min-width:50px');
        $grid->column('classify.title', '品类')->style('max-width:200px');
        $grid->column('seller_id', '销售员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('classify.status','品类状态')->bool([1 => true, 2 => false])->style('max-width:200px');
        $grid->exporter(new ShopifySkuCCSExporter());
        $grid->fixHeader();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportShopifySkuCCS());
        });
        $grid->paginate(10);
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter){
                $filter->equal('country_id','国家')->select(SysCountry::options());
                $filter->equal('classify_id', '品类')->select(ProductClassify::options());
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('seller_id','销售员')->select(User::getSellerOptions(FALSE));
            });
        });

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new ProductShopifySkuCCS());

        $form->tools(function (Form\Tools $tools){
            $tools->disableView();
            $tools->disableDelete();
        });

        $form->hidden('id');
        $form->select('country_id', '国家')->options(SysCountry::options())->required()->setWidth(5);
        $form->select('classify_id', '品类')->options(ProductClassify::options())->required()->setWidth(5);
        $form->select('seller_id','销售员')->options(User::getSellerOptions(False))
        ->default(Admin::user()->id)->rules('required')->required()->setWidth(5);

        if($form->isCreating()){
            $form->disableCreatingCheck(false);
        }
        $form->saving(function (Form $form) {
            // 判断是否是新增操作
            if ($form->isCreating()) {
                $count = ProductShopifySkuCCS::query()
                    ->where('country_id', $form->country_id)
                    ->where('classify_id', $form->classify_id)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该平台下,品类+国家已存在');
                    return back()->withInput();
                }
            }else{
                $count = ProductShopifySkuCCS::query()
                    ->where('id','<>',$form->model()->id)
                    ->where('country_id',$form->country_id)
                    ->where('classify_id',$form->classify_id)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该平台下,品类+国家已存在');
                    return back()->withInput();
                }
            }
        });

        $form->saved(function (Form $form) {
            //更新,回写产品销售
            ProductShopifySkuCCS::upSellerId($form->model()->country_id, $form->model()->classify_id, $form->model()->seller_id);
        });

        return $form;
    }

}
